using wnity.zframework;
using cframework.SDKUtil;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using static cframework.SDKUtil.SDKUtil_WX;

public class WxLoading : MonoBehaviour
{
    public WXOpenDataImage openDataImage;
    public WxAd wxAd;

    private void Awake()
    {
        StartCoroutine(StartLoading());
    }

    IEnumerator StartLoading()
    {
        {
            Debug.Log($"wxsdk init start");
            var w = SDKUtil_WX.instance.InitSDK().WithResult();
            yield return w;
            var initResult = (ISDKUtil.InitResult)w.Current;
            if (!initResult.success)
            {
                Debug.LogError($"wxsdk init failed: {initResult.code}");
                yield break;
            }

            Debug.Log($"wxsdk init success");
        }
    }

    IEnumerator Login()
    {
        Debug.Log($"wxsdk login start");
        var w = SDKUtil_WX.instance.Login().WithResult();
        yield return w;
        var loginResult = (ISDKUtil.LoginResult)w.Current;

        if (!loginResult.success)
        {
            Debug.LogError($"wxsdk login failed: {loginResult.code} | {loginResult.errMsg} | {loginResult.errno}");
            yield break;
        }
        Debug.Log($"wxsdk login success");
    }

    private void OnGUI()
    {
        GUI.skin.button.fontSize = 60;
        GUILayout.Button("");
        if (GUILayout.Button("login"))
        {
            StartCoroutine(Login());
        }

        if (GUILayout.Button("show openData"))
        {
            openDataImage.gameObject.SetActive(true);
        }

        if (GUILayout.Button("hide openData"))
        {
            openDataImage.gameObject.SetActive(false);
        }

        if (GUILayout.Button("share"))
        {
            var sharesdkData = new SDKShareData()
            {
                title = "test",
                imageUrl = "https://mmgame.qpic.cn/image/5f9144af9f0e32d50fb878e5256d669fa1ae6fdec77550849bfee137be995d18/0",
            };
            SDKUtil_WX.instance.Share(sharesdkData);
        }


        if (GUILayout.Button("showAd"))
        {
            StartCoroutine(ShowAd());
        }


        if (GUILayout.Button("GetSetting"))
        {
            StartCoroutine(GetSetting());
        }

        string auth = "scope.userInfo";
        if (GUILayout.Button($"check scope auth: {auth}"))
        {
            Debug.Log($"check scope auth: {auth} => {SDKUtil_WX.instance.ContainsAuth(auth)}");
        }

        if (GUILayout.Button($"GetUserInfo"))
        {
            StartCoroutine(GetUserInfo());
        }

        if (GUILayout.Button("GetUserInfoByAuthorize"))
        {
            StartCoroutine(GetUserInfoByAuthorize());
        }

        if (GUILayout.Button($"GetUserInfoByCreateUserInfoButton"))
        {
            StartCoroutine(GetUserInfoByCreateUserInfoButton());
        }

    }


    IEnumerator GetUserInfoByCreateUserInfoButton()
    {
        { 
            var w = SDKUtil_WX.instance.CreateUserInfoButton(new RectInt(0, 0, Screen.width, Screen.height), false).WithResult();
            yield return w;
            var result = (cframework.SDKUtil.SDKUtil_WX.GetUserInfoResult)w.Current;

            Debug.Log("CreateUserInfoButton reuslt: " + (result != null? result.nickname: "failed"));
        }

        //{ 
        //    var w2 = SDKUtilWX.instance.GetUserInfo().WithResult();
        //    yield return w2;
        //    var result2 = (GetUserInfoResult)w2.Current;

        //    if (!string.IsNullOrEmpty(result2.errMsg))
        //    {
        //        Debug.LogError(result2.errMsg);
        //    }
        //    else
        //    {
        //        Debug.Log($"userinfo: {result2.name} | {result2.avatarUrl}");
        //    }
        //}

        SDKUtil_WX.instance.DestroyUserInfoButton();
    }

    IEnumerator GetUserInfoByAuthorize()
    {
        {
            var w = SDKUtil_WX.instance.AuthorizeUserInfo().WithResult();
            yield return w;
            var result = (AuthorizeResult)w.Current;

            if (!string.IsNullOrEmpty(result.errMsg))
            {
                Debug.LogError(result.errMsg);

                yield break;
            }

            Debug.Log($"authorized");
        }

        { 
            var w = SDKUtil_WX.instance.GetUserInfo().WithResult();
            yield return w;
            var result = (GetUserInfoResult)w.Current;

            if(!string.IsNullOrEmpty(result.errMsg))
            {
                Debug.LogError(result.errMsg);
            }
            else
            {
                Debug.Log($"userinfo: {result.nickname} | {result.avatarUrl}");
            }
        }
    }

    IEnumerator GetUserInfo()
    {
        {
            var w = SDKUtil_WX.instance.GetUserInfo().WithResult();
            yield return w;
            var result = (GetUserInfoResult)w.Current;

            if (!string.IsNullOrEmpty(result.errMsg))
            {
                Debug.LogError(result.errMsg);
            }
            else
            {
                Debug.Log($"userinfo: {result.nickname} | {result.avatarUrl}");
            }
        }
    }

    IEnumerator GetSetting()
    {
        var w = SDKUtil_WX.instance.GetSetting().WithResult();
        yield return w;
        var result = (bool)w.Current;

        Debug.Log($"GetSetting: {result}");
    }

    IEnumerator ShowAd()
    {
        if(!wxAd.isInited)
        {
            Debug.Log("ad init failed: ");
            yield break;
        }

        var w = wxAd.ShowAd().WithResult();
        yield return w;
        var success = (bool)w.Current;

        Debug.Log("ShowAd get reward: " + success);
    }
}
